﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.ComponentModel;
using B.S.DataBase.MyProject.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;


namespace B.S.DataBase.Myproject.Helper
{
    public static class ObjectExtension
    {
        /// <summary>
        /// IsNotNull 是不空吗？ true 是的 false 不是 
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsNotNull(this string? o)
        {
            if (o != null && o.ToString() != "") return true;
            return false;
        }
        /// <summary>
        /// 字符串类型转化为日期
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static DateTime StringToDateTime(this string o)
        {
            if (o.IsNotNull())
            {
                DateTime.TryParse(o, out DateTime dt);
                return dt;
            }
            else
            {
                return Convert.ToDateTime("1900-01-01 00:00:00");
            }
        }
        /// <summary>
        /// 日期格式转化为字符串
        /// </summary>
        /// <param name="o"></param>
        /// <param name="format"></param>
        /// <param name="empty"></param>
        /// <returns></returns>
        public static string DateTimeToString(this DateTime o, string? format = "yyyy-MM-dd HH:mm:ss", string? empty = "--")
        {
            if (o.ToString().IsNotNull())
            {
                return o.ToString(format);
            }
            else
            {
                return empty;
            }
        }
        public static string Description(this Enum value)
        {
            FieldInfo field = value.GetType().GetField(value.ToString());
            DescriptionAttribute attribute = field.GetCustomAttribute<DescriptionAttribute>();
            return attribute == null ? value.ToString() : attribute.Description;
        }
    }


    public interface IUnitOfWork : IDisposable
    {
        Task BeginTransactionAsync();
        Task CommitTransactionAsync();
        Task RollbackTransactionAsync();
    }

    // 在EF Core中的实现示例
    public class UnitOfWork : IUnitOfWork
    {
        private readonly SDbContext _context;
        private IDbContextTransaction _transaction;

        public UnitOfWork(SDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task BeginTransactionAsync()
        {
            // 添加null检查
            if (_context == null)
                throw new InvalidOperationException("DbContext is not initialized");

            if (_context.Database == null)
                throw new InvalidOperationException("Database provider is not configured");
            _transaction = await _context.Database.BeginTransactionAsync();
        }

        public async Task CommitTransactionAsync()
        {
            if (_transaction == null)
                throw new InvalidOperationException("Transaction is not started");

            await _context.SaveChangesAsync();
            await _transaction.CommitAsync();
        }

        public async Task RollbackTransactionAsync()
        {
            if (_transaction != null)
            {
                await _transaction.RollbackAsync();
            }
        }

        public void Dispose()
        {
            _transaction?.Dispose();
            _context?.Dispose();
        }
    }
}
